package com.activities;

import java.util.ArrayList;

import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.Utility.MessageManager;
import com.Utility.Utility;
import com.Utility.ValidateText;
import com.Utility.WebServiceCommunicator.WebServiceFlag;
import com.controls.ws.GmailWSControl;
import com.dialogs.InforConfirmDialog;
import com.dialogs.LoadingDialog;
import com.events.WebServiceCommunicatorListener;
import com.objects.ContactCheck;
import com.objects.ValidateTextResponse;
import com.senditcertified.mail.R;

public class ImportGmailActivity extends BaseActivity implements
		WebServiceCommunicatorListener, OnClickListener {
	private EditText mEdtGmailUsername;
	private EditText mEdtGmailPassword;
	private GmailWSControl mWSGmail;
	private LoadingDialog mDialog;
	private ValidateText mValidateText;
	private InforConfirmDialog mInforConfirmDialog;

	@Override
	protected int initLayout() {
		return R.layout.activity_import_gmail;
	}

	@Override
	protected void initViews() {
		mEdtGmailUsername = (EditText) findViewById(R.id.edt_gmail_username);
		mEdtGmailPassword = (EditText) findViewById(R.id.edt_gmail_password);
	}

	@Override
	protected void initComponents() {
		mValidateText = new ValidateText();

	}

	@Override
	protected void addListener() {
		findViewById(R.id.ln_import_gmail).setOnClickListener(this);

	}

	@Override
	protected void onDestroy() {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		if (mInforConfirmDialog != null) {
			mInforConfirmDialog.dismiss();
			mInforConfirmDialog = null;
		}
		if (mWSGmail != null) {
			mWSGmail.cancel();
			mWSGmail = null;
		}
		super.onDestroy();
	}

	@Override
	public void onConnectionOpen(WebServiceFlag flag) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		mDialog = new LoadingDialog(getParent());
		mDialog.setCancelable(false);
		mDialog.show();

	}

	@Override
	public void onConnectionError(WebServiceFlag flag, String error) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
		MessageManager.showToast(this, error);

	}

	@Override
	public void onConnectionDone(WebServiceFlag flag, String response) {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}

		if (flag == WebServiceFlag.IMPORT_GMAIL) {
			String returnValue = mWSGmail.parseImportGmail(response);
			MessageManager.showToast(this, returnValue);
		}

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.ln_import_gmail) {
			ValidateTextResponse mCheck = mValidateText.validateTextLength(
					mEdtGmailUsername, 1, 200);
			if (!mCheck.isOK()) {
				showInforConfirm("The Username was wrong!", mCheck.getMessage());
				return;
			}
			mCheck = null;
			mCheck = mValidateText
					.validateTextLength(mEdtGmailPassword, 1, 200);
			if (!mCheck.isOK()) {
				showInforConfirm("The Password was wrong!", mCheck.getMessage());
				return;
			}
			mCheck = null;
			if (mWSGmail == null)
				mWSGmail = new GmailWSControl(this, this);
			mWSGmail.importGmailContacts(Utility.getStringPreference(this,
					Utility.KEY_LOGIN_TOKEN, ""), mEdtGmailUsername.getText()
					.toString().trim(), mEdtGmailPassword.getText().toString()
					.trim());
		}

	}

	private void showInforConfirm(String title, String message) {
		if (mInforConfirmDialog != null) {
			mInforConfirmDialog.dismiss();
			mInforConfirmDialog = null;
		}
		mInforConfirmDialog = new InforConfirmDialog(getParent());
		mInforConfirmDialog.setTitle(title);
		mInforConfirmDialog.setMessage(message);
		mInforConfirmDialog.show();
	}

}
